package com.example.xm.adapter.DiscoverAdapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.xm.R;
import com.example.xm.bean.DiscoverBean.DiscoverPagerBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DiscoverPagerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<DiscoverPagerBean.DataBean.ListBean> list;

    public DiscoverPagerAdapter(Context context, ArrayList<DiscoverPagerBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.pager_item, parent,false);
            ViewHolder viewHolder = new ViewHolder(inflate);
            return viewHolder;
        } else if (viewType == 2) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.pager_itme1, parent,false);
            ViewHolder1 viewHolder1 = new ViewHolder1(inflate);
            return viewHolder1;
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.pager_item2, parent,false);
            ViewHolder2 viewHolder2 = new ViewHolder2(inflate);
            return viewHolder2;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        List<DiscoverPagerBean.DataBean.ListBean.FilePathListBean> images = list.get(position).getFilePathList();
        DiscoverPagerBean.DataBean.ListBean bean = list.get(position);
        int itemViewType = holder.getItemViewType();
        if (itemViewType == 1) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.tvDataPager.setText(bean.getTitle());
            viewHolder.tvDatePager.setText(bean.getCreateTime());
            Glide.with(context).load(images.get(0).getFilePath()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.ivImg1Pager);
            Glide.with(context).load(images.get(1).getFilePath()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.ivImg2Pager);
            Glide.with(context).load(images.get(2).getFilePath()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder.ivImg3Pager);
        } else if (itemViewType == 2) {
            ViewHolder1 viewHolder1 = (ViewHolder1) holder;
            viewHolder1.tvDataPagerItem1.setText(bean.getTitle());
            viewHolder1.tvDatePagerItem1.setText(bean.getCreateTime());
            Glide.with(context).load(images.get(0).getFilePath()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(viewHolder1.ivImgPagerItem1);
        }else {
            ViewHolder2 viewHolder2 = (ViewHolder2) holder;
            viewHolder2.tvDataPagerItem2.setText(bean.getTitle());
            viewHolder2.tvDatePagerItem2.setText(bean.getCreateTime());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        List<DiscoverPagerBean.DataBean.ListBean.FilePathListBean> filePathList = list.get(position).getFilePathList();
        if (filePathList.size() == 3) {
            return 1;
        } else if (filePathList.size() == 1) {
            return 2;
        } else {
            return 3;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_data_pager)
        TextView tvDataPager;
        @BindView(R.id.iv_img1_pager)
        ImageView ivImg1Pager;
        @BindView(R.id.iv_img2_pager)
        ImageView ivImg2Pager;
        @BindView(R.id.iv_img3_pager)
        ImageView ivImg3Pager;
        @BindView(R.id.ll_pager)
        LinearLayout llPager;
        @BindView(R.id.tv_date_pager)
        TextView tvDatePager;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_data_pager_item1)
        TextView tvDataPagerItem1;
        @BindView(R.id.tv_date_pager_item1)
        TextView tvDatePagerItem1;
        @BindView(R.id.iv_img_pager_item1)
        ImageView ivImgPagerItem1;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_data_pager_item2)
        TextView tvDataPagerItem2;
        @BindView(R.id.tv_date_pager_item2)
        TextView tvDatePagerItem2;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
